<template>
  <div class="detail">
    <a-table
      class="hte-table-content"
      :scroll="{x: true}"
      :columns="levelColumns"
      :data-source="levelListData"
      :rowKey="
        (record, index) => {
          return index
        }
      "
      :loading="tableLoading"
      :pagination="false"
      @change="handleTableChange"
      >
      <template slot="projectCycleAvg" slot-scope="text, record">
        <span>{{ record.projectTotal ? parseInt(record.projectCycleTotal/record.projectTotal) : 0 }}</span>
      </template>
      <template slot="timeNodeCompleteRate" slot-scope="text, record">
        <span>{{ record.timeNodeTotal ? (parseFloat(record.completeTotal/record.timeNodeTotal)*100).toFixed(2)+"%" : '0.00%'}}</span>
      </template>
      <template slot="middleCycleTotal" slot-scope="text, record">
        <span>{{ record.projectTotal ? parseInt(record.middleCycleTotal/record.projectTotal) : 0 }}</span>
      </template>
      <template slot="tryCycleTotal" slot-scope="text, record">
        <span>{{ record.projectTotal ? parseInt(record.tryCycleTotal/record.projectTotal) : 0 }}</span>
      </template>
      <template slot="acceptanceCycleTotal" slot-scope="text, record">
        <span>{{ record.projectTotal ? parseInt(record.acceptanceCycleTotal/record.projectTotal) : 0 }}</span>
      </template>
    </a-table>
  </div>
</template>

<script>
import { getAction, postAction } from '@/api/manage'

export default {
  name: 'ProjectLevel',
  props: {
    treeDeptId: {
      type: Number,
      required: false,
    },
    subDeptIdByAtypeRoot: {
      type: String,
      default: '',
      required: false,
    },
    deptName: {
      type: String,
      default: '',
      required: false,
    },
    projectClass: {
      type: String,
      default: '10',
      required: false,
    },
    searchYear: {
      type: String,
      default: undefined,
      required: false,
    },
    projectStatus: {
      type: Number,
      default: null,
      required: false,
    }
  },
  watch: {
      treeDeptId(newValue, oldValue) {
          this.treeDeptId = newValue
          if (newValue != oldValue) {
              this.getLevelData()
          }
      },
      projectClass(newValue, oldValue) {
          this.projectClass = newValue
          if (newValue != oldValue) {
              this.getLevelData()
          }
      },
      searchYear(newValue, oldValue) {
          this.searchYear = newValue
          if (newValue != oldValue) {
              this.getLevelData()
          }
      },
      projectStatus(newValue, oldValue) {
          this.projectStatus = newValue
          if (newValue != oldValue) {
              this.getLevelData()
          }
      },
  },
  data() {
    return {
      levelColumns: [
        {
          title: '项目等级',
          dataIndex: 'projectLevel',
          key: 'projectLevel',
          width: '70px',
          align: 'center',
        },
        {
          title: '项目数量',
          dataIndex: 'projectTotal',
          key: 'projectTotal',
          align: 'center',
          width: '70px',
        },
        {
          title: '项目最长周期(天)',
          dataIndex: 'maxProjectCycle',
          key: 'maxProjectCycle',
          align: 'center',
          width: '120px',
        },
        {
          title: '项目最短周期(天)',
          dataIndex: 'minProjectCycle',
          key: 'minProjectCycle',
          align: 'center',
          width: '120px',
        },
        {
          title: '总平均周期(天)',
          dataIndex: 'projectCycleAvg',
          key: 'projectCycleAvg',
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'projectCycleAvg' },
        },
        {
          title: '立项-中期平均周期(天)',
          dataIndex: 'middleCycleTotal',
          key: 'middleCycleTotal',
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'middleCycleTotal' },
        },
        {
          title: '中期-试产平均周期(天)',
          dataIndex: 'tryCycleTotal',
          key: 'tryCycleTotal',
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'tryCycleTotal' },
        },
        {
          title: '试产-投产上市平均周期(天)',
          dataIndex: 'acceptanceCycleTotal',
          key: 'acceptanceCycleTotal',
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'acceptanceCycleTotal' },
        },
        {
          title: '总节点完成率',
          dataIndex: 'timeNodeCompleteRate',
          key: 'timeNodeCompleteRate',
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'timeNodeCompleteRate' },
        },
        {
          title: '总延误天数',
          dataIndex: 'delayDaysTotal',
          key: 'delayDaysTotal',
          align: 'center',
          width: '120px',
        },
      ],
      levelListData: [],
      tableLoading: false,
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.getLevelData()
    },
    getLevelData() {
      this.tableLoading=true;
      postAction('/techProjectReport/queryTechProjectLevelSummary', {
        subDeptIdByAtypeRoot: this.subDeptIdByAtypeRoot,
        treeDeptId: this.treeDeptId,
        projectClass: this.projectClass,
        searchYear: this.searchYear,
        projectStatus: this.projectStatus,
      }).then((res) => {
        this.tableLoading=false;
        if (res.success) {
          this.levelListData=res.result||[];
        } else {
          this.$message.error(res.message)
        }
      })
    },
  },
  mounted() {
    this.getLevelData()
  }
}
</script>

<style lang="less" scoped>

</style>